package com.woniu.mgr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.mgr.controller.form.AccountModifyForm;
import com.woniu.mgr.model.Account;
import com.woniu.mgr.dao.AccountDao;
import com.woniu.mgr.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.mgr.util.BeanCopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户账号表（学员和教练） 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountDao, Account> implements AccountService {

    @Resource
    private AccountDao accountDao;

    @Override
    public IPage<Account> list(Integer page, Integer size) {
        //平台对学员进行分页查询
        Page<Account> page1 = new Page<>(page, size);
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        Page<Account> page2 = accountDao.selectPage(page1, wrapper);
        return page2;
    }

    @Override
    public void accountModify(AccountModifyForm accountModifyForm) {
        //平台修改学员状态为可用或不可用（账号）
        Account account = BeanCopyUtil.copyObject(accountModifyForm, Account.class);
        updateById(account);
    }

    @Override
    public IPage<Account> listUser(String condition, Integer page, Integer size) {
        IPage<Account> accountPage = new Page<>(page, size);
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", "学员");
        if (!"".equals(condition) || condition != null) {
            IPage<Account> iPage = accountDao.selectPage(accountPage, queryWrapper);
            return iPage;
        } else {
            queryWrapper.eq("username", condition)
                    .or().eq("nickname", condition)
                    .or().eq("gender", condition)
                    .or().eq("age", condition)
                    .or().eq("state", condition);
            IPage<Account> iPage = accountDao.selectPage(accountPage, queryWrapper);
            return iPage;
        }


    }
}
